package com.xiaomaoguai.mybatisplus.utils;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

/**
 * FreemarkerHelper工具类
 */
public class FreemarkerTemplateHelper {

	private static final String DEFAULT_ENCODING = "UTF-8";

	/**
	 * @return
	 * @throws TemplateException Configuration
	 * @Name: getConfig
	 * @Description:Freemarker设置
	 * @author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年6月6日下午7:17:23
	 */
	public static Configuration getConfig() throws TemplateException {
		Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
		cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23));
		cfg.setDefaultEncoding(DEFAULT_ENCODING);
		// SET PROPERTIES
		Prop prop = new Prop("freemarker.properties");
		cfg.setSettings(prop.getProperties());
		return cfg;
	}

	/**
	 * @param dir
	 * @param templateName
	 * @param encoding
	 * @param root
	 * @param targetFile
	 * @return
	 * @throws IOException
	 * @throws TemplateException String
	 * @Name: loadTemplateByFile
	 * @Description:从文件路径加载模板
	 * @author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年6月6日下午7:17:51
	 */
	public static String loadTemplateByFile(File dir, String templateName, String encoding, Map<String, Object> root,
	                                        String targetFile) throws IOException, TemplateException {
		Configuration cfg = getConfig();
		String result = null;
		ByteArrayOutputStream os = null;
		Writer out = null;
		FileOutputStream fileOutputStream = null;
		try {
			cfg.setDirectoryForTemplateLoading(dir);
			Template template = cfg.getTemplate(templateName, encoding);
			os = new ByteArrayOutputStream();
			File target = new File(targetFile);
			if (!target.getParentFile().exists()) {
				target.getParentFile().mkdirs();
				target.createNewFile();
			}
			fileOutputStream = new FileOutputStream(target);
			out = new OutputStreamWriter(fileOutputStream, encoding);
			template.process(root, out);
			out.flush();
			result = os.toString(encoding);
		} finally {
			if (os != null) {
				os.close();
			}
			if (out != null) {
				out.close();
			}
			if (fileOutputStream != null) {
				fileOutputStream.close();
			}
		}
		return result;
	}

	/**
	 * @param basePackagePath
	 * @param templateName
	 * @param encoding
	 * @param root
	 * @param targetFile
	 * @return
	 * @throws IOException
	 * @throws TemplateException String
	 * @Name: loadTemplateByContext
	 * @Description:从classpath路径加载模板
	 * @author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年6月6日下午7:18:14
	 */
	public static String loadTemplateByContext(String basePackagePath, String templateName, String encoding,
	                                           Map<String, String> root, String targetFile) throws IOException, TemplateException {
		Configuration cfg = getConfig();
		String result = null;
		ByteArrayOutputStream os = null;
		Writer out = null;
		try {
			cfg.setClassLoaderForTemplateLoading(FreemarkerTemplateHelper.class.getClassLoader(), basePackagePath);
			Template template = cfg.getTemplate(templateName, encoding);
			os = new ByteArrayOutputStream();
			out = new OutputStreamWriter(new FileOutputStream(targetFile), encoding);
			template.process(root, out);
			out.flush();
			result = os.toString(encoding);
		} finally {
			if (os != null) {
				os.close();
			}
			if (out != null) {
				out.close();
			}
		}
		return result;
	}
}
